require('./index.scss')

import $ from 'jquery'

$(document).ready(function() {
	console.log(window.location.search)
	var fileList
	const cid = window.location.search.split('=')[1]
	console.log(cid)
	// // 当用户选择图片触发事件
	$('#uploadfile').on('input', function() {
		console.log('showloading')
		readFile(this);
	})

	//用户确认上传图片
	$('#submit-btn').click(() => {
		$('#loading').show()
		var data = new FormData();
		data.append("file", fileList[0])
		data.append('cid', cid)
		console.log(data)
		$.ajax({
			url: 'https://test.51xeld.com/app/file/ScanUpload',
			type: 'POST',
			data: data,
			cache: false,
			processData: false,
			contentType: false,
			success: function(res) {
				// source = res.picAddr;
				$('#loading').hide()
				$('#rechoose').hide()
				$('#img-box').hide()
				$('#success-icon').show()
				console.log(res)
			},
			error: function(res) {
				$('#loading').hide()
				$('#rechoose').hide()
				$('#img-box').hide()
				$('#failed-icon').show()
			}
		});
	})
	//处理图片并添加都dom中的函数
	var readFile = function(obj) {
		console.log('readFile running')
		// 获取input里面的文件组
		fileList = obj.files;
		console.log(fileList)
		var reader = new FileReader();
		reader.readAsDataURL(fileList[0]);
		// 当文件读取成功时执行的函数
		reader.onload = function(e) {
			$('#img-box').show()
			$('#input-file-box').hide()
			$('#rechoose').show()
			$('#choose-btn').hide()
			$('#img-box').html($('<img src="' + this.result + '" />'));
		}

	}
})
